package LABS.lab5;

/**************************************************************
* Class ManipulateQueue
* @author Jeff Thomas - Thomas007
* @vesion 1.0.0
* Date: 03.04.2008
* Copyright 2008, Jeff Thomas
***************************************************************/

import java.util.*;

public class ManipulateQueue<E>
{
	private Queue<E> elements;
/**************************************************************
* Maniupulate Queue
* Task:	1. Create a Queue using LinkedList
* Side Effects: None
***************************************************************/
	public ManipulateQueue() 
	{
		elements = new LinkedList<E>();
	}
/**************************************************************
* n to Front
* @param n - integer of element to be moved
* Task:	1. Create two empty queues for storage
*			2. Traverse the list and increment count for each element
*			3. When we reach the element to be moved, place it in the
*				result queue
*			4. Place all other elements in a temp queue
*			5. Add all temp elements to the result queue
* Side Effects: None
***************************************************************/
	public void nToFront(int n)
	{
		Queue<E> temp = new LinkedList<E>();
		Queue<E> result = new LinkedList<E>();
		int count = 0;
		while (elements.peek() != null)
		{
			count++;
			if (count == n)
			{
				result.offer(elements.poll());
			}
			else
			{
				temp.offer(elements.poll());
			}
		}
		while (temp.peek() != null)
		{
			result.offer(temp.poll());
		}
		elements = result;  
	}
/**************************************************************
* Reverse Using n to Front
* Task:	1. Move through the queue and place each element at the
*				front.
* Side Effects: None
***************************************************************/	
	public void reverseUsingNtoFront()
	{
		for (int i = 1; i <= elements.size(); i++)
		{
			nToFront(i);
		}
	}
/**************************************************************
* Reverse Queue
* Task:	1. Create an empty stack
*			2. Move each element in the queue to that stack
*			3. Move each element from the stack back to the queue
* Side Effects: None
***************************************************************/	
	public void reverseQueue()
	{
		Stack<E> temp = new Stack<E>();
		while (elements.peek() != null)
		{
			temp.push(elements.poll());
		}
		while (!temp.empty())
		{
			elements.offer(temp.pop());
		}
	}
/**************************************************************
* Add
* @param object - object to add to the queue
* Task:	1. Offer each object to the queue
* Side Effects: None
***************************************************************/
	public void add(E object)
	{
			elements.offer(object);
	}
/**************************************************************
* To String
* @return - The queue in string format seperated by |
* Task:	1. Create a string builder
*			2. Grab each element from the queue and seperate with |
* Side Effects: None
***************************************************************/
	public String toString()
	{
		StringBuilder sb = new StringBuilder();
		sb.append("| ");
		for (E nextObject : elements)
		{
			sb.append(nextObject);
			sb.append(" | ");
		}
		return sb.toString();
	}
}	
/**************************************************************
* I, Jeff Thomas, certify that each part of this program was
* written by me, except as noted within the code.
*
*						_________________________
***************************************************************/